/**
 * 
 */
package info.niwota.web.util;

import info.niwota.web.Debug;
import info.niwota.web.bt.BluetoothAddress;
import info.niwota.web.bt.BluewebSettings;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author qiangli
 * 
 */
public class SettingsDialog extends JDialog implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JLabel label1 = new JLabel("Server port:");

	private JLabel label2 = new JLabel("Proxy port:");

	private JLabel label3 = new JLabel("Proxy peer:");

	private JTextField field1 = new JTextField();

	private JTextField field2 = new JTextField();

	private JComboBox list = null;

	private BluewebSettings cfg;

	private BluetoothAddress[] addresses;

	public SettingsDialog(Frame owner, boolean modal,
			List<BluetoothAddress> addresses, BluewebSettings cfg) {
		super(owner, modal);

		this.cfg = cfg;
		this.addresses = addresses.toArray(new BluetoothAddress[0]);
		this.list = new JComboBox(this.addresses);

		init();
	}

	private void init() {
		this.setTitle("Settings");

		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(6, 1));
		panel.add(label1);
		panel.add(field1);
		panel.add(label2);
		panel.add(field2);
		panel.add(label3);
		panel.add(list);

		if (cfg.proxyAddress != null && addresses.length > 0) {
			for (int i = 0; i < addresses.length; i++) {
				String addr = addresses[i].getAddress();
				if (addr == null) {
					continue;
				}
				if (addr.equals(cfg.proxyAddress)) {
					list.setSelectedIndex(i);
					break;
				}
			}
		}
		list.addActionListener(this);

		final JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(this);
		cancelButton.setActionCommand("cancel");
		//
		final JButton okButton = new JButton("Ok");
		okButton.setActionCommand("ok");
		okButton.addActionListener(this);

		JPanel buttons = new JPanel();
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.LINE_AXIS));
		buttons.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
		buttons.add(Box.createHorizontalGlue());
		buttons.add(cancelButton);
		buttons.add(Box.createRigidArea(new Dimension(10, 0)));
		buttons.add(okButton);

		Container contentPane = getContentPane();
		contentPane.add(panel, BorderLayout.CENTER);
		contentPane.add(buttons, BorderLayout.PAGE_END);
		
		field1.setText(String.valueOf(cfg.serverPort));
		field2.setText(String.valueOf(cfg.proxyPort));
		
		pack();
		
		center();
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		String act = ae.getActionCommand();
		if (Debug.DEBUG) {
			System.out.println("action: " + act);
		}
		if (act != null) {
			if (act.equals("ok")) {
				saveValues();
				//
				dispose();
				return;
			} else if (act.equals("cancel")) {
				dispose();
				return;
			}
		}
	}

	private void saveValues() {
		if (Debug.DEBUG) {
			System.out.println("saveValues: " + cfg);
		}
		
		try {
			int p1 = Integer.parseInt(field1.getText());
			
			if (p1 > 0) {
				cfg.serverPort = p1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			int p2 = Integer.parseInt(field2.getText());
			if (p2 > 0) {
				cfg.proxyPort = p2;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		BluetoothAddress adr = (BluetoothAddress) list.getSelectedItem();
		cfg.proxyAddress = adr.getAddress();
	}
	
	private void center() {
		int width = getWidth();
		int height = getHeight();
		Dimension dim = getToolkit().getScreenSize();
		setLocation((dim.width - width) / 2, (dim.height - height) / 2);
		setSize(width, height);
	}
	
}
